import { useEffect, useState } from 'react';
import { message } from 'antd';
import { groupBy } from 'lodash';
import moment from 'moment';
import { useModel, useRequest } from 'umi';

import NoticeIcon from './NoticeIcon';
import styles from './index.less';

export type GlobalHeaderRightProps = {
    fetchingNotices?: boolean;
    onNoticeVisibleChange?: (visible: boolean) => void;
    onNoticeClear?: (tabName?: string) => void;
};

const getNoticeData = (
    notices: AircasKB.NoticeIconItem[],
): Record<string, AircasKB.NoticeIconItem[]> => {
    if (!notices || notices.length === 0 || !Array.isArray(notices)) {
        return {};
    }

    const newNotices = notices.map((notice) => {
        const newNotice = { ...notice };

        if (newNotice.datetime) {
            newNotice.datetime = moment(notice.datetime as string).fromNow();
        }

        if (newNotice.id) {
            newNotice.key = newNotice.id;
        }

        if (newNotice.extra && newNotice.status) {
            const color = {
                todo: '',
                processing: 'blue',
                urgent: 'red',
                doing: 'gold',
            }[newNotice.status];
            newNotice.extra = newNotice.extra as any;
        }

        return newNotice;
    });
    return groupBy(newNotices, 'type');
};

const getUnreadData = (noticeData: Record<string, AircasKB.NoticeIconItem[]>) => {
    const unreadMsg: Record<string, number> = {};
    Object.keys(noticeData).forEach((key) => {
        const value = noticeData[key];

        if (!unreadMsg[key]) {
            unreadMsg[key] = 0;
        }

        if (Array.isArray(value)) {
            unreadMsg[key] = value.filter((item) => !item.read).length;
        }
    });
    return unreadMsg;
};

const NoticeIconView: React.FC = () => {
    return <>123</>;
};

export default NoticeIconView;
